<?php 
defined('_JEXEC') or die('Restricted access'); 
JHTML::_('behavior.modal');
?>
<script type="text/javascript" src="components/com_sialpacadb/assets/calendar.js"></script>
<script type="text/javascript" src="components/com_sialpacadb/assets/moovalid-1.1.js"></script>
<script type="text/javascript">
window.addEvent('domready', function() {
		var fields = {
			nome:		'Required',			
			nascita : 'Required SDate',
			morte : 'SDate',
			sel_status: 'SelReq',
			sel_colore: 'SelReq',
			sel_tipo: 'SelReq',
			sel_sesso: 'SelReq',
			proprietario: 'Required',
			codallev: 'Codec'
		};
		
		var val = new validate('adminForm', fields, { 
			useAjaxSubmit:false,
			AjaxSubmitOptions: {
				evalScripts: true,
				onComplete: function(response) { 
					$('log').set('html',response);
				}
			}
		});
		// faccio overraid del tasto salva
		submitbutton  = function (mytask){			
			$('task').set('value',mytask);
			$('adminForm').fireEvent('submit');
		};
			
});

var toggleModal = function(backgroundColour, options) {

    if ($("modal")) {
        $("modal").dispose();
        return false;
    }

    var options = $merge({
        zIndex: 100,
        opacity: .9,
        events: $empty()
    }, options);

    if (!$type(backgroundColour) && !$("modal"))
        return false;

    return new Element("div", {
        id: "modal",
        styles: {
            position: "absolute",
            top: 0,
            left: 0,
            width: window.getScrollWidth(),
            height: window.getScrollHeight(),
            background: backgroundColour,
            "z-index": options.zIndex
        },
        opacity: options.opacity,
        events: options.events
    }).inject(document.body);
} // end toggleModal
function mkmodal(who,what){
	toggleModal("#aaa");
	$('modal').addEvent('dblclick', function(){ $("modal").fade(0);(function() { toggleModal(); }).delay(500);} );
	
	// crea il div contenitore
	var mydiv= new Element("div",{
		 styles:{
			 position:"absolute",
			 top:200,
			 left:200,
			 background: '#ddd',
			 width:'30%',
			 border:"medium solid grey"
			 },
		id:"boxato"
		
	});
	// crea tasto chiudi
	var mybutton = new Element("button",{type:"button",id:"chiudi",events:{click:function(){$("modal").fade(0);toggleModal();}},styles:{background:"#fff",margin: "10px"}});
	mybutton.set('text',"Chiudi");
	// crea il form 
	mydiv.set("text","tabella: "+who);
	var myform = new Element("form",{id:"myform",action:"index.php?option=com_sialpacadb&format=raw",method:"post",name:"myform"});
	// crea tabella dinamica
	if (who == "dna"){
	   <?php echo $this->cctable_dna; ?>	
	}
	if (who == "fibra"){
	   <?php echo $this->cctable_fibra; ?>	
	}
    
	var mysubmit = new Element("input",{type:"submit",name:"mysubmit",id:"mysubmit",value:"Salva",styles:{background:"#fff",margin: "10px"}});
	mydiv.inject('modal');
	mybutton.inject(mydiv,"top");
	mytable.inject(myform);
	// aggiunge i campi necessari al post
	myform.grab(new Element("input",{type:"hidden",name:"option",value:"com_sialpacadb"}));
	myform.grab(new Element("input",{type:"hidden",name:"task",value:"saveRowPetax"}));
	myform.grab(new Element("input",{type:"hidden",name:"tablename",value:"jos_sialpaca_" + who}));
	//$(what).value = $('uid').value;	
	mysubmit.inject(myform);
	myform.inject(mydiv);
	var mycippa = new Element("div",{id:"response"});
	
	mycippa.grab(new Element("div",{id:"loading"}).set("text","Salvataggio in corso..."));
	mycippa.grab(new Element("div",{id:"success"}).set("text","OK Salvato"));
	mycippa.grab(new Element("div",{id:"complete"}).set("text","OK chiudi"));
	mycippa.grab(new Element("div",{id:"fail"}).set("text","Salvataggio fallito!!"));
	mycippa.inject(mydiv);
	
	$(what).set('value',$('uid').value);
	
	
	var fx = {
	'loading': new Fx.Style( 'loading', 'opacity',{ duration: 200 } ),
	'success': new Fx.Style( 'success', 'opacity',{ duration: 200 } ),
	'complete': new Fx.Style( 'complete', 'opacity',{ duration: 400 } ),
	'fail': new Fx.Style( 'fail', 'opacity',{ duration: 200 } )
	};
	var showHide = function( el ){
		fx.loading.set(0);
		(fx[ el ]).start(0,1);
		(function(){ (fx[ el ]).start(1,0); }).delay( 4000 );
	}
	
	$('mysubmit').addEvent('click',function(evt){
		new Event(evt).stop();
		$('myform').send({
			onRequest: function(){
			// Show loading div.
			fx.loading.start( 0,1 );
		},
		onSuccess: function(response){
			// Hide loading and show success for 3 seconds.		
			if (response){
				if($(who)){$(who).set('value',response);}
			}
			showHide( 'success' );
			toggleModal();
			
	        	
		},
		
		onFailure: function(){
			// Hide loading and show fail for 3 seconds.
			showHide( 'fail' );
		}
		
		});
		}
	);
	
}


function submitf(pressbutton){
	if (pressbutton) {
		document.adminForm.task.value=pressbutton;
	}
	alert(typeof(document.adminForm.onsubmit));
	alert(document.adminForm.onsubmit.toSource());
	if (typeof document.adminForm.onsubmit == "object") {
		console.log(document.adminForm.onsubmit);
		console.log(pressbutton);
		
		//document.adminForm.onsubmit();
		
	}
	if (typeof document.adminForm.onsubmit == "function") {
		alert("qui");
		//document.adminForm.onsubmit();
		
	}
	//document.adminForm.submit();
}

</script>

<style>
		#loading, #success, #fail, #complete {
			background:#819F70;
			padding: 3px;
			width: 130px;
			position:absolute;
			top: 0;
			right: 0;
			visibility:hidden;
		}
		#success{
			background:yellow;
		}
		#fail{
			background: red;
		}
		#complete{
			background: green;
		}
		.errorElement{ border:1px #FF0000 solid;  } 
		.errorText{ color:red; }
</style>
<div id="log"></div>
<form action="<?php echo $this->action; ?>" method="post" name="adminForm" id="adminForm" >
	<div style="width:100%;float:left;">
		<?php echo $this->cctable; ?>
	</div>
<input type="hidden" name="option" value="com_sialpacadb" />
<!-- <input type="hidden" name="id" value="Test" /> -->
<input type="hidden" id='task' name="task" value="" />
<input type="hidden" name="pagemode" value="<?php echo $this->pagemode; ?>" />
<input type="hidden" name="tablename" value="<?php echo $this->tabsel; ?>" />

</form>

